package com.shop.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.shop.constant.Constants;
import com.shop.dao.ShoppingCartDao;
import com.shop.dao.ShoppingDao;
import com.shop.dao.impl.ShoppingCartDaoImpl;
import com.shop.dao.impl.ShoppingDaoImpl;
import com.shop.entity.Order;
import com.shop.entity.OrderDetail;
import com.shop.entity.PayMessage;
import com.shop.exception.DAOException;
import com.shop.factory.ObjectFactory;
import com.shop.service.ShoppingService;
import com.shop.transaction.TransactionManager;
import com.shop.util.DateTimeUtil;

public class ShoppingServiceImpl implements ShoppingService {

    private ShoppingDao shoppingDao = (ShoppingDaoImpl) ObjectFactory.getInstance(Constants.SHOPPING_DAO_KEY);
    private ShoppingCartDao shoppingCartDao = (ShoppingCartDaoImpl) ObjectFactory.getInstance(Constants.SHOPPING_CART_DAO_KEY);

    @Override
    public int BuyNow(int u_id, double o_total, String gd_id, String s_id) throws DAOException {
        TransactionManager tm = new TransactionManager();
        int o_id = 0;
        int result = 0;
        try {
            //开启事务
            tm.openTransaction();
            //下面是业务处理代码
            //生成订单
            Order o = new Order();
            o.setU_id(u_id);
            o.setO_total(o_total);
            o.setO_pay(0);
            o.setO_status(0);
            String time = DateTimeUtil.getSysTime();
            o.setO_starttime(time);
            //提交订单
            result = shoppingDao.createOrder(o);
            if(result == 0) {
                throw new DAOException();
            }
            //返回订单id
            o_id = shoppingDao.selectId(u_id,time);
            //将购物车中选中的商品删除
            //1.查询购物车id
//            int sc_id = shoppingCartDao.getCartId(u_id);
//            if(gd_id.size() != s_id.size()) {
//                throw new DAOException();
//            }
            OrderDetail od = new OrderDetail();
            od.setO_id(o_id);
            od.setOd_status(2); //默认为2，表示订单商家还未处理
//            for (int i=0; i<gd_id.size(); i++) {
                //2.开始删除购物车中的商品
//                shoppingCartDao.deleteGoodsByIdAndState(sc_id,gd_id.get(i).toString());
                //3.删除完毕，开始往订单详情表中添加数据
            od.setGd_id(Integer.parseInt(gd_id));
            od.setS_id(Integer.parseInt(s_id));
            shoppingDao.createOrderDetail(od);
//            }

            //业务处理代码写完后需要提交
            tm.commit();
        } catch (DAOException e) {
            //如果报错，这里需要回滚
            tm.rollback();
            e.printStackTrace();
            throw new DAOException(e);
        }
        return o_id;
    }

    @Override
    public int order(int u_id, double o_total, JSONArray gd_id, JSONArray s_id) throws DAOException {
        TransactionManager tm = new TransactionManager();
        int o_id = 0;
        int result = 0;
        try {
            //开启事务
            //tm.openTransaction();
            //下面是业务处理代码
            //生成订单
            Order o = new Order();
            o.setU_id(u_id);
            o.setO_total(o_total);
            o.setO_pay(0);
            o.setO_status(0);
            String time = DateTimeUtil.getSysTime();
            o.setO_starttime(time);
            //提交订单
            result = shoppingDao.createOrder(o);
            if(result == 0) {
                throw new DAOException();
            }
            //返回订单id
            o_id = shoppingDao.selectId(u_id,time);
            //将购物车中选中的商品删除
            //1.查询购物车id
            int sc_id = shoppingCartDao.getCartId(u_id);
            if(gd_id.size() != s_id.size()) {
                throw new DAOException();
            }
            OrderDetail od = new OrderDetail();
            od.setO_id(o_id);
            od.setOd_status(2); //默认为2，表示订单商家还未处理
            for (int i=0; i<gd_id.size(); i++) {
                //2.开始删除购物车中的商品
                shoppingCartDao.deleteGoodsByIdAndState(sc_id,gd_id.get(i).toString());
                //3.删除完毕，开始往订单详情表中添加数据
                od.setGd_id(Integer.parseInt(gd_id.get(i).toString()));
                od.setS_id(Integer.parseInt(s_id.get(i).toString()));
                shoppingDao.createOrderDetail(od);
            }

            //业务处理代码写完后需要提交
            //tm.commit();
        } catch (DAOException e) {
            //如果报错，这里需要回滚
            //tm.rollback();
            e.printStackTrace();
            throw new DAOException(e);
        }
        return o_id;
    }

    @Override
    public PayMessage getPayMessage(int o_id) throws DAOException {

        PayMessage pm = new PayMessage();
        //获取订单total
        double total = shoppingDao.getTotal(o_id);

        pm.setO_id(o_id);
        pm.setO_total(total);

        return pm;
    }

    @Override
    public void changecPayState(String o_id) throws DAOException {
        try {
            int result = shoppingDao.changecPayState(o_id);
            if(result == 0) {
                throw new DAOException();
            }
        } catch (DAOException e) {
            e.printStackTrace();
            throw new DAOException();
        }
    }

}
